$('.to-login').click(function(){
    $('.to-login').removeClass('active-login');
    $('.to-login').addClass('account-login');
    $(this).addClass('active-login');
    $(this).removeClass('account-login');
})
var login =  {
    init:function () {
        this.loginType = 1;
        this.initEvent();
    },
    initEvent:function() {
        //注册事件
        var self=this;
        $("#password").keydown(function (e) {
            if(e.keyCode==13){
                self.loginSend();
            }
        });
        $('.login-btn').click(function(){
            self.loginSend();
        });
        $('.active-login').click(function () {
            self.loginType = 1;
        });
        $('.account-login').click(function () {
            self.loginType = 2;
        });
        $('.bx-log').click(function () {
            $('.pull-right-btn').css('display','none');
        });
        $('.account-log').click(function () {
            $('.pull-right-btn').css('display','block');
        });
    },

    loginSend:function () {
        $('.err-info').html('');
        if (window.location.href.split('&')[1]) {
            if(window.location.href.split('&')[1].split('/')[4]){
                path = window.location.href.split('&')[1].split('/')[3]+'/'+window.location.href.split('&')[1].split('/')[4];
            }else{
                path = window.location.href.split('&')[1].split('/')[3];
            }
        } else {
            path = '';
        }
        var mobile = $("#email").val();
        var password = $("#password").val();
        // var reg = new RegExp(/^[A-Za-zd]+([-_.][A-Za-zd]+)*@([A-Za-zd]+[-.])+[A-Za-zd]{2,5}$ /);
        // var reg = new RegExp(/^((\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)$/);//手机号验证
        var reg = new RegExp(/^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/);//邮箱验证
        if (!mobile.match(reg)) {
            $('.err-info').html('请输入正确的邮箱');
            return;
        }
        if (!password) {
            $('.err-info').html('请输入密码');
            return;
        }
        var userdata;
        if (this.loginType == 1){
            userdata = {
                email: mobile,
                password: md5(password),
                repassword: md5(password)
            };
        }else {
            userdata = {
                email: mobile,
                password: password,
                repassword: password
            };
        }
        if (mobile && password) {
            $.ajax({
                url: '/login.json?loginType='+this.loginType,
                type: 'post',
                data: JSON.stringify(userdata),
                contentType: 'application/json;charset=UTF-8',
                success: function (data) {
                    if (data.stat == 'OK') {
                        location.href = '/' + path;
                    } else {
                        $('.err-info').html("用户名和密码不匹配");
                    }
                },
                error: function (err) {
                    $('.err-info').html(JSON.parse(err.responseText).errors[0].msg);
                }
            })
        }
    }
 };

login.init();

